/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: GPU/NPU send/recv typical interfaces
 */
/*
 * 该特性代码不涉及开源
 */

#ifndef INTERFACE_HCCL_H
#define INTERFACE_HCCL_H

#include "acl/acl_base.h"
#include "stream_pub.h"
#include "hccl/base.h"

#define GID_LENGTH 16

#ifdef __cplusplus
extern "C" {
#endif

typedef struct AscendQPInfoDef {
    uint32_t qpn;
    uint32_t gid_idx;
    uint8_t gid[GID_LENGTH];
    uint32_t psn;
} AscendQPInfo;

typedef struct AscendQPQosDef {
    uint32_t tc;
    uint32_t sl;
} AscendQPQos;

HcclResult hcclAscendRdmaInit();
HcclResult hcclAscendRdmaDeInit();

/* QPInfo: out */
HcclResult hcclCreateAscendQP(AscendQPInfo* ascendQPInfo);
/* localQPinfo: in; remoteQPInfo: in */
HcclResult hcclModifyAscendQP(AscendQPInfo* localQPInfo, AscendQPInfo* remoteQPInfo);
HcclResult hcclModifyAscendQPEx(AscendQPInfo* localQPInfo, AscendQPInfo* remoteQPInfo, AscendQPQos* qpQos);
HcclResult hcclDestroyAscendQP(AscendQPInfo* ascendQPInfo);

typedef struct AscendMrInfoDef {
    uint64_t addr;  // in: starting address of mr
    uint64_t size;  // in: size of mr
    uint32_t key;   // out: local addr access key
} AscendMrInfo;

HcclResult hcclAllocWindowMem(void **ptr, uint64_t size);
HcclResult hcclFreeWindowMem(void *ptr);

HcclResult hcclAllocSyncMem(int32_t **ptr);
HcclResult hcclFreeSyncMem(int32_t *ptr);

HcclResult hcclRegisterMem(AscendMrInfo* memInfo);
HcclResult hcclDeRegisterMem(AscendMrInfo* memInfo);

typedef struct AscendSendRecvInfoDef {
    AscendQPInfo* localQPinfo;
    AscendMrInfo* localWindowMem;
    AscendMrInfo* remoteWindowMem;
    AscendMrInfo* localSyncMemPrepare;
    AscendMrInfo* localSyncMemDone;
    AscendMrInfo* localSyncMemAck;
    AscendMrInfo* remoteSyncMemPrepare;
    AscendMrInfo* remoteSyncMemDone;
    AscendMrInfo* remoteSyncMemAck;
    uint32_t immData = 0; // 默认为0， 不发送立即数，当imma不为0时，支持按照immData作为立即数发送
} AscendSendRecvInfo;

HcclResult HcclSendByAscendQP(void* sendBuf, uint64_t count, HcclDataType dataType,
    AscendSendRecvInfo* sendRecvInfo, aclrtStream stream);
HcclResult HcclRecvByAscendQP(void* recvBuf, uint64_t count, HcclDataType dataType,
    AscendSendRecvInfo* sendRecvInfo, aclrtStream stream);

struct HcclErrCqeInfo {
    uint32_t status;
    uint32_t qpn;
    struct timeval time;
};

/**
* @brief 异构场景QP状态迁移接口，并支持按照QP粒度设置RDMA qos，包括sl、tc。
* @param localQPInfo (in): 本地QP信息。
* @param remoteQPInfo (in):对端QP信息。
* @param remoteQPInfo (in):对本端QP配置Qos，包含tc，sl值。tc值有效范围[0，255]，且必须是4的倍数。sl值有效范围[0，7]。
*/
HcclResult HcclGetCqeErrInfoList(struct HcclErrCqeInfo *infoList, uint32_t *num);

/**
* @brief 按照QP粒度获取RDMA error cqe 信息。
* @param infoList (out): error cqe 数组指针。
* @param num (out):获取error cqe的个数，不大于128。
*/
HcclResult HcclGetCqeErrInfoListByQpn(uint32_t qpn, struct HcclErrCqeInfo *infoList, uint32_t *num);
#ifdef __cplusplus
}  // extern "C"
#endif

#endif